package com.green.model.uitl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class EnumUtil {
	/**
	 * 转换成枚举
	 * @param <T>
	 * @param fieldName
	 * @param enumCls
	 * @param eqValue
	 * @return
	 * @throws Exception
	 */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public static <T>T asEnum(String fieldName,Class enumCls,String eqValue) 
    {
    	if(eqValue==null)return null;
    	
    	Field[] fs=enumCls.getFields();
    	String value=null;
    	String otherName;
    	Enum e=null;
    	Method m;
		try {
			m = enumCls.getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1));
			for (int j = 0; j < fs.length; j++) {
	    		 value=fs[j].getName();
	    		 e=Enum.valueOf(enumCls, value);
	    		 otherName=m.invoke(e)+"";
	    		 if(eqValue.equals(otherName))
	    		 {
	    			 return (T) e;
	    		 }
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
    	return null;
    	
    }

}
